package core.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.heizhu.appfuncore.R;

import java.util.List;

import core.android.deeplinks.deeplinking.AKDeeplink;
import core.android.deeplinks.deeplinking.annotations.DeeplinkQueryParameter;
import core.android.deeplinks.deeplinking.annotations.DeeplinkRoute;
import core.android.tools.AK;
import core.app.config.DeepLinkScheme;

/**
 * @作者: XQ
 * @创建时间：2016/8/5 15:17
 * @类说明: 同城fragment
 */

@DeeplinkRoute("neibor")
public class NeiborFragment extends Fragment {
    @DeeplinkQueryParameter("url")
    String mUrl;
    @DeeplinkQueryParameter("referrer")
    String mReferrer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AKDeeplink.INSTANCE.inject(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentview = inflater.inflate(R.layout.fragment_neibor, container, false);
        initView(contentview);
        return contentview;
    }

    private void initView(View contentview) {
        View testView = contentview.findViewById(R.id.btn);
        testView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AKDeeplink.INSTANCE.openURL(DeepLinkScheme.CATEGORY);
            }
        });
    }
    //配合activiy重写onbackpress调用此方法，实现手机点击返回键只回退webview不关闭activity
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item == null) {
            List<Fragment> fragments = getFragmentManager().getFragments();
            AK.d(fragments);
            if (fragments.size() > 1) {
                getFragmentManager().beginTransaction().remove(this).commit();
            } else {
                //如果是最后一个，直接关闭activity
                getActivity().finish();
            }
        }
        return super.onOptionsItemSelected(item);//item为null不影响
    }
}
